#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class RoomManaRcdProvider : CY.GFive.Core.Providers.DALProvider.IRoomManaRcdProvider
	{
		#region Sql
		private static readonly string SqlSelectRoomManaRcd = "SELECT [Id], [TeacherCode],[AddDate],[YearNum],[Term],[Month],[RoomCate],[RoomNum] FROM [RoomManaRcd] WHERE  [Id]=@Id";

		private static readonly string SqlInsertRoomManaRcd = "INSERT INTO [RoomManaRcd]([TeacherCode],[AddDate],[YearNum],[Term],[Month],[RoomCate],[RoomNum]) values(@TeacherCode,@AddDate,@YearNum,@Term,@Month,@RoomCate,@RoomNum);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateRoomManaRcd = "UPDATE [RoomManaRcd] SET [TeacherCode] = @TeacherCode,[AddDate] = @AddDate,[YearNum] = @YearNum,[Term] = @Term,[Month] = @Month,[RoomCate] = @RoomCate,[RoomNum] = @RoomNum WHERE  [Id]=@Id";

		private static readonly string SqlDeleteRoomManaRcd = "DELETE [RoomManaRcd] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllRoomManaRcd = "SELECT [Id], [TeacherCode],[AddDate],[YearNum],[Term],[Month],[RoomCate],[RoomNum] FROM [RoomManaRcd]";
		#endregion

		#region IRoomManaRcdProvider Members
		public Core.Business.RoomManaRcd Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectRoomManaRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.RoomManaRcd roomManaRcd = new Core.Business.RoomManaRcd();

				if (!reader.IsDBNull(0)) roomManaRcd.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) roomManaRcd.TeacherCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) roomManaRcd.AddDate = reader.GetDateTime(2);
				if (!reader.IsDBNull(3)) roomManaRcd.YearNum = reader.GetString(3);
				if (!reader.IsDBNull(4)) roomManaRcd.Term = reader.GetInt32(4);
				if (!reader.IsDBNull(5)) roomManaRcd.Month = reader.GetInt32(5);
				if (!reader.IsDBNull(6)) roomManaRcd.RoomCate = reader.GetString(6);
				if (!reader.IsDBNull(7)) roomManaRcd.RoomNum = reader.GetInt32(7);

				return roomManaRcd;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.RoomManaRcd roomManaRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, roomManaRcd.TeacherCode);
			sql.AddParameter("@AddDate", SqlDbType.DateTime, roomManaRcd.AddDate);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, roomManaRcd.YearNum);
			sql.AddParameter("@Term", SqlDbType.Int, roomManaRcd.Term);
			sql.AddParameter("@Month", SqlDbType.Int, roomManaRcd.Month);
			sql.AddParameter("@RoomCate", SqlDbType.NVarChar, roomManaRcd.RoomCate);
			sql.AddParameter("@RoomNum", SqlDbType.Int, roomManaRcd.RoomNum);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertRoomManaRcd);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) roomManaRcd.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.RoomManaRcd roomManaRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, roomManaRcd.TeacherCode);
			sql.AddParameter("@AddDate", SqlDbType.DateTime, roomManaRcd.AddDate);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, roomManaRcd.YearNum);
			sql.AddParameter("@Term", SqlDbType.Int, roomManaRcd.Term);
			sql.AddParameter("@Month", SqlDbType.Int, roomManaRcd.Month);
			sql.AddParameter("@RoomCate", SqlDbType.NVarChar, roomManaRcd.RoomCate);
			sql.AddParameter("@RoomNum", SqlDbType.Int, roomManaRcd.RoomNum);
			sql.AddParameter("@Id", SqlDbType.Int, roomManaRcd.Id);
			sql.ExecuteSql(SqlUpdateRoomManaRcd);
		}

		public void Delete(Core.Business.RoomManaRcd roomManaRcd)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, roomManaRcd.Id);
			sql.ExecuteSql(SqlDeleteRoomManaRcd);
		}

		public IList<Core.Business.RoomManaRcd> GetAllRoomManaRcd()
		{
			IList<Core.Business.RoomManaRcd> roomManaRcdlist = new List<Core.Business.RoomManaRcd>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllRoomManaRcd);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.RoomManaRcd roomManaRcd = new Core.Business.RoomManaRcd();

					if (!reader.IsDBNull(0)) roomManaRcd.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) roomManaRcd.TeacherCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) roomManaRcd.AddDate = reader.GetDateTime(2);
					if (!reader.IsDBNull(3)) roomManaRcd.YearNum = reader.GetString(3);
					if (!reader.IsDBNull(4)) roomManaRcd.Term = reader.GetInt32(4);
					if (!reader.IsDBNull(5)) roomManaRcd.Month = reader.GetInt32(5);
					if (!reader.IsDBNull(6)) roomManaRcd.RoomCate = reader.GetString(6);
					if (!reader.IsDBNull(7)) roomManaRcd.RoomNum = reader.GetInt32(7);

					roomManaRcd.MarkOld();
					roomManaRcdlist.Add(roomManaRcd);
				}
				reader.Close();
			}
			return roomManaRcdlist;
		}
		#endregion
	}
}
